package cn.bonoon.kernel.menus;

import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.util.PathUtil;

/**
 * 平台或子系统样式的定义
 * @author jackson
 *
 */
public class StyleDefinitionImpl implements StyleDefinition{
	
//	private String menuStyle;
	private int menuWidth;
	private int topHeight;
	private int footHeight;
	
	private String logo;
	private String foot;
	private String name;
	private String thumbnail;

	/** 资源目录，如果有定义则有，如果没有定义，则表示没有 */
	private String resourceFolder;
	
	private ServerResourceResolver resourceResolver;

	private ModuleDefinition parent;
	
	public void init(PlatformContext context, ModuleDefinition parent){
		internalInit(context.getResourceResolver(), parent);
		StyleDefinitionImpl source = context.getStyle();
		//如果子系统没有定义目录，则直接使用平台定义的目录，注意这个是有顺序的
		this.resourceFolder = StringHelper.get(this.resourceFolder, source.resourceFolder);
		
		this.foot = StringHelper.get(this.foot, source.foot);
		this.name = StringHelper.get(this.name, source.name, parent.getName());
		
		this.logo = internalParseImage(this.logo, source.logo);
		this.thumbnail = internalParseImage(this.thumbnail, source.thumbnail);
		
		if(menuWidth < MIN_PX) menuWidth = source.menuWidth;
		if(topHeight < MIN_PX) topHeight = source.topHeight;
		if(footHeight < MIN_PX) footHeight = source.footHeight;
	}
	
	public void init(ServerResourceResolver resourceResolver, ModuleDefinition parent){
		internalInit(resourceResolver, parent);
		String name = parent.getName();
		this.logo = internalParseImage(this.logo, null);
		this.thumbnail = internalParseImage(this.thumbnail, null);
		
		this.name = StringHelper.get(this.name, name);
		this.foot = StringHelper.get(this.foot, name);

		if(menuWidth < MIN_PX) menuWidth = MENU_WIDTH;
		if(topHeight < MIN_PX) topHeight = TOP_HEIGHT;
		if(footHeight < MIN_PX) footHeight = FOOT_HEIGHT;
		
	}
	
	private void internalInit(ServerResourceResolver resourceResolver, ModuleDefinition parent){
		this.parent = parent;
		this.resourceResolver = resourceResolver;
		this.resourceFolder = resourceResolver.resource(this.resourceFolder);
	}
	
	private String internalParseImage(String path, String dpath){
		if(null == path) return dpath;
		if(path.startsWith("/")) return resourceResolver.resource(path);
		return PathUtil.path(resourceFolder, path);
	}

	@Override
	public String resource(String path) {
		return PathUtil.path(resourceFolder, path);
	}
	
	public void setResourceFolder(String resourceFolder) {
		this.resourceFolder = resourceFolder;
	}

	public int getMenuWidth() {
		return menuWidth;
	}

	public void setMenuWidth(int menuWidth) {
		this.menuWidth = menuWidth;
	}

	public int getTopHeight() {
		return topHeight;
	}

	public void setTopHeight(int topHeight) {
		this.topHeight = topHeight;
	}

	public int getFootHeight() {
		return footHeight;
	}

	public void setFootHeight(int footHeight) {
		this.footHeight = footHeight;
	}
//
//	public String getSystemLogo() {
//		return systemLogo;
//	}
//
//	public void setSystemLogo(String systemLogo) {
//		this.systemLogo = systemLogo;
//	}
//
//	public String getFootName() {
//		return footName;
//	}
//
//	public void setFootName(String footName) {
//		this.footName = footName;
//	}
//
//	public String getSystemTitle() {
//		return systemTitle;
//	}
//
//	public void setSystemTitle(String systemTitle) {
//		this.systemTitle = systemTitle;
//	}
//
//	public String getSystemName() {
//		return systemName;
//	}
//
//	public void setSystemName(String systemName) {
//		this.systemName = systemName;
//	}
//
//	public String getJs() {
//		return js;
//	}
//
//	public String getCss() {
//		return css;
//	}
//
//	public void setResourcesCss(List<String> resourcesCss) {
//		this.resourcesCss = resourcesCss;
//	}
//
//	public void setResourcesJs(List<String> resourcesJs) {
//		this.resourcesJs = resourcesJs;
//	}

	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getFoot() {
		return foot;
	}

	public void setFoot(String foot) {
		this.foot = foot;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getThumbnail() {
		return thumbnail;
	}

	public void setThumbnail(String thumbnail) {
		this.thumbnail = thumbnail;
	}
	
	public ModuleDefinition getParent() {
		return parent;
	}
}
